package config

import (
	"os"

	"gopkg.in/yaml.v3"
)

type MysqlConfig struct {
	IP       string `yaml:"ip"`
	Port     int    `yaml:"port"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Database string `yaml:"database"`
}

type MongodbConfig struct {
	IP       string `yaml:"ip"`
	Port     int    `yaml:"port"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
}

type LogConfig struct {
	FilePath string `yaml:"filepath"`
}

type AllConfig struct {
	Mode      string    `yaml:"mode"`
	LogConfig LogConfig `yaml:"log"`
	DBs       struct {
		MysqlCfg   MysqlConfig   `yaml:"mysql"`
		MongodbCfg MongodbConfig `yaml:"mongodb"`
	} `yaml:"dbs"`
}

func LoadConfig(path string) (*AllConfig, error) {

	data, err := os.ReadFile(path)
	if err != nil {
		return nil, err
	}

	var allCfg AllConfig
	if err = yaml.Unmarshal(data, &allCfg); err != nil {
		return nil, err
	}

	return &allCfg, nil
}
